package com.engine.kq.wfset.action;

import com.engine.kq.biz.KQFLowEventLogBiz;
import com.engine.kq.biz.KQFlowActiontBiz;
import com.engine.kq.biz.KQOvertimeLogBiz;
import com.engine.kq.log.KQLog;
import com.engine.kq.wfset.bean.SplitBean;
import com.engine.kq.wfset.util.KQFlowOvertimeUtil;
import com.engine.kq.wfset.util.SplitActionUtil;
import com.engine.kq.wfset.util.SplitDutyActionUtil;
import com.google.common.collect.Maps;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.resource.ResourceComInfo;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.workflow.WorkflowComInfo;

/**
 * 兼容E8的加班生成调休action
 */
public class KqPaidLeaveAction extends BaseBean implements Action {
  private KQLog kqLog = new KQLog();

  @Override
  public String execute(RequestInfo request) {
    this.writeLog("KqPaidLeaveAction", "do action on request:" + request.getRequestid());
    String requestid = request.getRequestid();
    kqLog.info("do KqPaidLeaveAction on requestid:"+requestid);
    int requestidInt = Util.getIntValue(requestid, 0);

    String workflowid = request.getWorkflowid();
    String formid = new WorkflowComInfo().getFormId(workflowid);
    KQFLowEventLogBiz kqfLowEventLogBiz = new KQFLowEventLogBiz();
    String logKey = "|key|requestid|"+requestid;

    try {
      List<SplitBean> splitBeans = new ArrayList<SplitBean>();

      KQFlowActiontBiz kqFlowActiontBiz = new KQFlowActiontBiz();
      KQFlowOvertimeUtil kqFlowOvertimeUtil = new KQFlowOvertimeUtil();
      ResourceComInfo rci = new ResourceComInfo();
      DateTimeFormatter datetimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
      RecordSet rs = new RecordSet();
      String proc_set_sql = "select * from kq_att_proc_set where field001 = ? and field002 = ? ";
      rs.executeQuery(proc_set_sql, workflowid,formid);

      Map<String,Object> eventLogMap = Maps.newHashMap();
      eventLogMap.put("proc_set_sql", proc_set_sql);
      eventLogMap.put("workflowid", workflowid);
      eventLogMap.put("formid", formid);

      String uuid = "";
      if(rs.next()){
        String proc_set_id = rs.getString("id");
        //得到这个考勤流程设置是否使用明细
        String usedetails = rs.getString("usedetail");
        int kqtype = Util.getIntValue(rs.getString("field006"));
        Map<String, String> map = new HashMap<>();
        if(requestidInt > 0){
          map.put("requestId", "and t.requestId = " + requestidInt);
        }
        Map<String, String> sqlMap = kqFlowActiontBiz.handleSql(proc_set_id, usedetails, requestidInt,kqtype,map);

        uuid = kqfLowEventLogBiz.logEvent("request|Creatorid|"+request.getCreatorid(),eventLogMap,"KqPaidLeaveAction|触发生成调休action"+logKey);
        Map<String,String> result = kqFlowOvertimeUtil.handleKQOvertimeAction(sqlMap, splitBeans, datetimeFormatter, Util.getIntValue(workflowid), requestidInt, rci,uuid);
        if(!result.isEmpty()){
          String error = Util.null2String(result.get("message"));
          request.getRequestManager().setMessageid("666" + request.getRequestid() + "999");
          request.getRequestManager().setMessagecontent(error);
          return Action.FAILURE_AND_CONTINUE;
        }
      }
      // 生成调休 在归档的时候再单独处理下加班规则第二种情况
      SplitActionUtil.handleOverTimeAction(splitBeans, requestid,false,uuid);
    } catch (Exception e) {
      writeLog(e);
      request.getRequestManager().setMessageid("11111" + request.getRequestid() + "22222");
      request.getRequestManager().setMessagecontent("加班流程【加班时间转为可调休时间action】报错，请联系管理员！");
      StringWriter errorsWriter = new StringWriter();
      e.printStackTrace(new PrintWriter(errorsWriter));
      kqLog.info("加班流程【加班时间转为可调休时间action】报错，请联系管理员！"+errorsWriter.toString());
      Map<String,Object> eventLogMap = Maps.newHashMap();
      eventLogMap.put("action_error", errorsWriter.toString());
      String uuid = kqfLowEventLogBiz.logEvent("request|Creatorid|"+request.getCreatorid(),eventLogMap,"KqPaidLeaveAction|触发生成调休action"+logKey);
      return Action.FAILURE_AND_CONTINUE;
    }

    return Action.SUCCESS;

  }

}
